﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Infrastructure;
using Super.Entities;
using Super.Common.Validate;
using Super.Common.Security;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class AdminController : BaseController
    {
        /// <summary>
        /// 管理员首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            using (var adp = new DbContextAdapter())
            {
                var adminService = new BaseService<Administrator>(adp);
                var adminList = adminService.Repository.GetAll().ToList();
                return View(adminList);
            }

        }


        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt();
            if(id>0)
            {
                var adp = new DbContextAdapter();
                var adminService = new BaseService<Administrator>(adp);
                var admin = adminService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(admin);
            }
            return View();
        }

        [HttpPost]
        public JsonResult Edit(Administrator Model)
        {
            var check = Query.Post("isSuper");
            var adp = new DbContextAdapter();
            var adminService = new BaseService<Administrator>(adp);
            if(Model.ID==0)
            {
                Model.LastLoginTime = DateTime.Now;
            }

            if (!string.IsNullOrEmpty(Model.PassWord))
            {
                Model.PassWord = MD5.GetMD5(Model.PassWord);

            }
            Model.IsSuperAdmin = check == "true";
            adminService.Repository.InsertOrUpdate(Model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "admin_index");

        }


    }
}
